﻿using Modules.Corp;
using PmSoft;
using System.Security.Authentication;

namespace ManagementApi.Mvc.ApplicationContexts;

/// <summary>
/// 企业请求上下文
/// </summary>
[UseDI(ServiceLifetime.Scoped)]
public class CorpApplicationContext : ProjectUserApplicationContext
{

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="serviceProvider"></param>
    public CorpApplicationContext(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }


    private CorpInfo? _corpInfo = null;

    /// <summary>
    /// 当前登录的企业信息
    /// </summary>
    public CorpInfo CurrentCorp
    {
        get
        {
            if (_corpInfo != null)
                return (CorpInfo)_corpInfo;

            if (CurrentUser == null || CurrentUser is not CorpInfo corpInfo)
                throw new AuthenticationException("当前非企业用户类型");
            _corpInfo = corpInfo;
            return _corpInfo;
        }
    }

    /// <summary>
    /// 当前登录是否为当前项目的总包
    /// </summary>
    /// <returns></returns>
    public bool IsLumpContractorCorp()
    {
        if (CurrentUser == null || CurrentUser is not CorpInfo corpInfo)
            return false;
        if (CurrentProjectInfo.LumpContractorCorpId == corpInfo.CorpId)
            return true;
        return false;
    }

    /// <summary>
    /// 当前登录是否为当前项目的建设单位
    /// </summary>
    /// <returns></returns>
    //public bool IsBuildCorp()
    //{
    //    if (CurrentUser == null || CurrentUser is not ConstructorInfo constructorInfo)
    //        return false;
    //    if (CurrentProjectInfo.BuildCorpId == constructorInfo.ConstructorId)
    //        return true;
    //    return false;
    //}
}
